﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Providers;

namespace Services
{
    /// <summary>
    /// Manager the user configuration/settings of SuperQuery
    /// </summary>
    public class UserConfigurationService : ServiceBase<IUserConfigurationProvider>
    {
        #region Public

        /// <summary>
        /// Initialise the UserConfiguration service
        /// </summary>
        public UserConfigurationService()
        {
            Initialise();
        }

        /// <summary>
        /// The currently loaded user configuratration
        /// </summary>
        public static Models.UserConfiguration UserConfig
        {
            get;
            set;
        }

        /// <summary>
        /// Load the users configuraation data
        /// </summary>
        /// <param name="errorMsg">Any error message that is generated as part of this process</param>
        /// <returns>True if the configuration data was loaded successfully</returns>
        public bool LoadUserConfiguration(out string errorMsg)
        {
            UserConfigurationService.UserConfig = Provider.LoadUserConfiguration(out errorMsg);
            return (string.IsNullOrEmpty(errorMsg));   // True if error message is blank so config was loaded without error
        }

        /// <summary>
        /// Save the current state of the users configuration
        /// </summary>
        /// <param name="errorMsg">Any error message that is generated as part of this process</param>
        /// <returns>True if the save was successful, otherwise false</returns>
        public bool SaveUserConfiguration(out string errorMsg)
        {
            return Provider.SaveUserConfiguration(UserConfigurationService.UserConfig, out errorMsg);
        }

        #endregion Public

        #region Protected

        /// <summary>
        /// Create a provider of the type used by the service
        /// </summary>
        /// <returns>A provider</returns>
        protected override IUserConfigurationProvider CreateProvider()
        {
            return ProviderFactory.CreateUserConfigurationProvider();
        }

        #endregion Protected
    }
}
